package com.julis.activity.check;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.Constant;
import com.baseutils.SpfUtils;
import com.baseutils.TimeUtils;
import com.bean.TradeMessage;
import com.github.czy1121.view.RoundButton;
import com.julis.R;
import com.julis.adapter.DOWN_Adapter;
import com.julis.db.Z_ObuInfoDao;
import com.julis.ui.ListViewForScrollView;
import com.julis.ui.horizon.HorizontalProgressViewModel;
import com.julis.ui.horizon.Node;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.thread.CheckOBUThread;
import com.util.JSONUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 确认设备信息
 */
public class N_QRSBXX3_Activity extends AppCompatActivity {


    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.vin_code)
    TextView vin_code;
    @BindView(R.id.obu_code)
    TextView obu;
    @BindView(R.id.textView14)
    TextView textView14;
    @BindView(R.id.bt_bzq)
    RoundButton btYz;
    @BindView(R.id.bt_next)
    RoundButton btByz;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    Z_ObuInfoDao z_obuInfoDao;
    String uuid;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.textView16)
    TextView textView16;
    @BindView(R.id.obutradevalue)
    TextView obutradevalue;
    @BindView(R.id.lanya)
    TextView lanya;
    @BindView(R.id.blecheckvalue)
    TextView blecheckvalue;
    @BindView(R.id.vininputvalue)
    TextView vininputvalue;
    @BindView(R.id.obuzijianvalue)
    TextView obuzijianvalue;
    @BindView(R.id.check_layout)
    LinearLayout check_layout;
    @BindView(R.id.layout_vininput)
    RelativeLayout Layout_VinInput;
    @BindView(R.id.layout_vinwrite)
    RelativeLayout Layout_Vinwrite;
    @BindView(R.id.layout_bluetooth)
    RelativeLayout layout_bluetooth;
    @BindView(R.id.layout_zijian)
    RelativeLayout layout_zijian;
    //是否检测蓝牙
    boolean Check_Bluetooth, Inpue_Obu;
    String OBU;
    int stepvalue = 0;
    KProgressHUD dialog1, dialog2, dialog3, dialog4;
    String check_ble = "1";
    boolean Check_Vin;
    boolean Check_kl15;
    @BindView(R.id.tv_obujiaoyi)
    TextView tvObujiaoyi;
    @BindView(R.id.lv)
    ListViewForScrollView lv;
    List<String> ccList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrsbxx);
        Inpue_Obu = (boolean) SpfUtils.get(this, Constant.Inpue_Obu, true);
        Check_Bluetooth = (boolean) SpfUtils.get(this, Constant.Check_Bluetooth, true);
        Check_Vin = (boolean) SpfUtils.get(this, Constant.Check_Vin, true);
        Check_kl15 = (boolean) SpfUtils.get(this, Constant.Check_kl15, true);
        ButterKnife.bind(this);
        initadapter();
        initview();

    }

    private void initview() {
        check_layout.setVisibility(View.GONE);
        uuid = getIntent().getStringExtra("uuid");
        z_obuInfoDao = new Z_ObuInfoDao(this);

        //检查Vin
        if (Check_Vin) {
            Layout_VinInput.setVisibility(View.VISIBLE);
            Layout_Vinwrite.setVisibility(View.VISIBLE);
            stepvalue = 3;
        } else {
            Layout_VinInput.setVisibility(View.GONE);
            Layout_Vinwrite.setVisibility(View.GONE);
            stepvalue = 2;
        }
        //检查蓝牙
        if (Check_Bluetooth) {
            layout_bluetooth.setVisibility(View.VISIBLE);
            check_ble = "1";
        } else {
            layout_bluetooth.setVisibility(View.GONE);
            check_ble = "2";
        }
        if (Check_kl15) {
            layout_zijian.setVisibility(View.VISIBLE);
        } else {
            layout_zijian.setVisibility(View.GONE);
        }


        String vin = (String) SpfUtils.get(N_QRSBXX3_Activity.this, Constant.VIN_Code, "");
        vin_code.setText("VIN码：" + vin);
        OBU = (String) SpfUtils.get(N_QRSBXX3_Activity.this, Constant.OBU_Code, "");
        obu.setText("OBU MAC号：" + OBU);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        HorizontalProgressViewModel model = new HorizontalProgressViewModel();
        model.setViewUp(this, recyclerView, getProgressList());
        if (!TextUtils.isEmpty(OBU)) {
            checks(OBU);
        } else {
            checks("");
        }

    }


    public void showdialog1() {
        dialog1 = KProgressHUD.create(N_QRSBXX3_Activity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在检测OBU")
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }

    public void showdialog2() {
        dialog2 = KProgressHUD.create(N_QRSBXX3_Activity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在检测蓝牙")
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }

    public void showdialog3() {
     String   data=Constant.ManufacturerName;
        dialog3 = KProgressHUD.create(N_QRSBXX3_Activity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在进行OBU"+data)
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }

    public void showdialog4() {
        dialog4 = KProgressHUD.create(N_QRSBXX3_Activity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在写入VIN号")
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }

    boolean can = true;

    /**
     * 模拟节点数据
     * node1.nodeStatus: 0 已完成状态  1正在处理状态  -1待处理状态
     *
     * @return
     */
    private List<Node> getProgressList() {
        List<Node> list = new ArrayList<>();


        if (Check_Vin) {
            Node node1 = new Node();
            node1.nodeName = "录入VIN";
            node1.nodeStatus = 0;
            list.add(node1);
        }
        if (Inpue_Obu) {
            Node node2 = new Node();
            node2.nodeName = "录入OBU";
            node2.nodeStatus = 0;
            list.add(node2);
        }

        Node node4 = new Node();
        node4.nodeName = "设备检测";
        node4.nodeStatus = 0;
        list.add(node4);

        return list;

    }


    @OnClick({R.id.img_back, R.id.bt_bzq, R.id.bt_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                Log.e("zzz", "xx" + can);
                if (can) {
                    finish();
                }

                break;
            case R.id.bt_bzq:

                checks(OBU);
                break;
            case R.id.bt_next:
                finishproject();

                break;
        }
    }

    String kill15;
    @SuppressLint("HandlerLeak")
    private Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            TradeMessage obj = (TradeMessage) msg.obj;
            String gettime = TimeUtils.gettime();
            Log.e("ZZZ", "xx" + msg.what);
            switch (msg.what) {
                case 1:
                    if (dialog1 != null && dialog1.isShowing()) {
                        dialog1.dismiss();
                    }
                    kill15 = "false";
                    obuzijianvalue.setText("OBU自检异常，" + obj.getMessage());
                    obuzijianvalue.setTextColor(getResources().getColor(R.color.red));
                    break;
                case 2:
                    if (dialog1 != null && dialog1.isShowing()) {
                        dialog1.dismiss();
                    }
                    kill15 = "true";
                    obuzijianvalue.setText("成功");
                    obuzijianvalue.setTextColor(getResources().getColor(R.color.ym_green));

                    break;
                case 3:
                    if (dialog2 != null && dialog2.isShowing()) {
                        dialog2.dismiss();
                    }
                    ble_check_success = "false";
                    blecheckvalue.setText("蓝牙检测异常，" + obj.getMessage());
                    blecheckvalue.setTextColor(getResources().getColor(R.color.red));
                    break;
                case 4:
                    if (dialog2 != null && dialog2.isShowing()) {

                        dialog2.dismiss();
                    }
                    ble_check_success = "true";
                    blecheckvalue.setText("成功");
                    blecheckvalue.setTextColor(getResources().getColor(R.color.ym_green));

                    break;
                case 5:
                    ccList.add("失败");
                    if (!TextUtils.isEmpty(OBU)) {

                    }else {
                        obu.setText("OBU MAC号：" + "");
                        SpfUtils.put(N_QRSBXX3_Activity.this, Constant.OBU_Code, "");
                    }

//
                    String message = obj.getIndex();
                    Log.e("test", message);
                    String  data=Constant.ManufacturerName;
                    down_adapter.addItem(data+"第" + message + "次：失败," + obj.getMessage());
                    down_adapter.notifyDataSetChanged();
                    break;
                case 6:
                    String mac = obj.getMac();
                    if (TextUtils.isEmpty(OBU)) {
                        obu.setText("OBU MAC号：" + obj.getMac());
                    }


                    SpfUtils.put(N_QRSBXX3_Activity.this, Constant.OBU_Code, mac);
                    String message2 = obj.getIndex();
                    Log.e("test", message2);
                    ccList.add("成功");
                     data=Constant.ManufacturerName;
                    down_adapter.addItem(data+"第" + message2 + "次：成功");
                    down_adapter.notifyDataSetChanged();
                    break;
                case 7:
                    showdialog1();
                    break;
                case 8:
                    showdialog2();
                    break;
                case 9:
                    showdialog3();
                    int tradetime = (int) SpfUtils.get(N_QRSBXX3_Activity.this, "lsxtradenumber", 1);
                      data= "OBU"+ Constant.ManufacturerName ;
                    tvObujiaoyi.setText(data+":" + tradetime + "次");
                    break;
                case 10:
                    if (dialog1 != null && dialog1.isShowing()) {
                        dialog1.dismiss();
                    }
                    if (dialog2 != null && dialog2.isShowing()) {
                        dialog2.dismiss();
                    }
                    if (dialog3 != null && dialog3.isShowing()) {
                        dialog3.dismiss();
                    }
                    if (dialog4 != null && dialog4.isShowing()) {
                        dialog4.dismiss();
                    }
                    check_layout.setVisibility(View.VISIBLE);
                    can = true;
                    break;
                case 11:
                    if (dialog4 != null && dialog4.isShowing()) {
                        dialog4.dismiss();
                    }
                    vin_write_success = "true";
                    vininputvalue.setText("成功");
                    vininputvalue.setTextColor(getResources().getColor(R.color.ym_green));
                    break;
                case 13:
                    if (dialog4 != null && dialog4.isShowing()) {
                        dialog4.dismiss();
                    }
                    vin_write_success = "false";
                    vininputvalue.setText("VIN写入异常" + obj.getMessage());
                    vininputvalue.setTextColor(getResources().getColor(R.color.red));
                    break;
                case 12:
                    showdialog4();
                    break;

                default:
                    break;
            }

        }
    };


    public void checks(String OBU) {
        can = false;
        ccList.clear();
        down_adapter.clear();
        check_layout.setVisibility(View.GONE);
        obu.setText("OBU MAC号：" + OBU);
        obutradevalue.setText(" ");
        vininputvalue.setText(" ");
        blecheckvalue.setText(" ");
        obuzijianvalue.setText(" ");
        String data= "OBU"+ Constant.ManufacturerName ;
        tvObujiaoyi.setText(data);
        vininputvalue.setTextColor(getResources().getColor(R.color.ym_green));
        obutradevalue.setTextColor(getResources().getColor(R.color.ym_green));
        blecheckvalue.setTextColor(getResources().getColor(R.color.ym_green));
        obuzijianvalue.setTextColor(getResources().getColor(R.color.ym_green));
        CheckOBUThread activateOBUThread = new CheckOBUThread(N_QRSBXX3_Activity.this, OBU, handler1);
        activateOBUThread.start();
        String gettime = TimeUtils.gettime();
        SpfUtils.put(N_QRSBXX3_Activity.this, Constant.OBU_CheckTime, gettime);
    }

    String ble_check_success;
    String vin_write_success;

    public void finishproject() {

        String obu_Trade_success = JSONUtils.ObjectToJson(ccList);
        Log.e("test", "数据" + obu_Trade_success);
        String vincode = (String) SpfUtils.get(N_QRSBXX3_Activity.this, Constant.VIN_Code, "");
        String obu_code = (String) SpfUtils.get(N_QRSBXX3_Activity.this, Constant.OBU_Code, "");
        String vin_input_time = (String) SpfUtils.get(N_QRSBXX3_Activity.this, Constant.VIN_InputTime, "");
        String obu_input_time = (String) SpfUtils.get(N_QRSBXX3_Activity.this, Constant.OBU_InputTime, "");
        String vin_input_success = (String) SpfUtils.get(N_QRSBXX3_Activity.this, Constant.VIN_InputSucess, "");
        String obu_input_success = (String) SpfUtils.get(N_QRSBXX3_Activity.this, Constant.OBU_InputSucess, "");
//        String obu_Trade_success = (String) SpfUtils.get(N_QRSBXX3_Activity.this, Constant.OBU_TradeSucess, "");

        if (!Check_Bluetooth) {
            ble_check_success = "";
        }
        if (!Check_kl15) {
            kill15 = "";
        }

        if (!Check_Vin) {
            vin_write_success = "";
        }
//        else {
//            vin_write_success = "true";
//        }

        String device_check_time = (String) SpfUtils.get(N_QRSBXX3_Activity.this, Constant.OBU_CheckTime, "");
        String check_people = (String) SpfUtils.get(N_QRSBXX3_Activity.this, Constant.Name, "");
        z_obuInfoDao.addOBU(vincode, obu_code, vin_input_time,
                obu_input_time, vin_input_success,
                obu_input_success, obu_Trade_success,
                ble_check_success, vin_write_success,
                device_check_time, check_people, kill15);
        Toast.makeText(this, "记录保存成功！", Toast.LENGTH_SHORT).show();
        finish();
    }

    DOWN_Adapter down_adapter;

    private void initadapter() {
        down_adapter = new DOWN_Adapter();
        down_adapter.addActivity(this);
        lv.setAdapter(down_adapter);
    }
}
